package frame;

import clsaa.Biglotto;
import data.ConfigBigLotto;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static frame.MainFrame.initpanelTable;

public class addOnelottoframe extends JFrame {

    private JTextField lotteryNumberField;
    private JTextField dateField;
    private JTextField frontArea1Field;
    private JTextField frontArea2Field;
    private JTextField frontArea3Field;
    private JTextField frontArea4Field;
    private JTextField frontArea5Field;
    private JTextField backArea1Field;
    private JTextField backArea2Field;

    public addOnelottoframe() {
        super("添加数据");

        // 设置窗口关闭操作
        setDefaultCloseOperation(2);

        // 设置窗口大小
        setSize(400, 300);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        // 计算JFrame的左上角应该放在屏幕上的位置
        int x = (screenSize.width - this.getWidth()) / 2;
        int y = (screenSize.height - this.getHeight()) / 2;

        // 设置JFrame的位置
        this.setLocation(x, y);
        // 创建面板和布局
        JPanel panel = new JPanel(new GridLayout(10, 2, 10, 10)); // 10行2列，水平和垂直间距均为10

        // 创建并添加标签和文本字段
        panel.add(new JLabel("期号:"));
        lotteryNumberField = new JTextField(10);
        panel.add(lotteryNumberField);

        panel.add(new JLabel("日期:"));
        dateField = new JTextField(10);
        panel.add(dateField);

        for (int i = 1; i <= 5; i++) {
            panel.add(new JLabel("前区" + i + ":"));
            JTextField frontAreaField = new JTextField(5);
            panel.add(frontAreaField);

            // 将文本字段保存到变量中（如果需要后续访问）
            if (i == 1) frontArea1Field = frontAreaField;
            if (i == 2) frontArea2Field = frontAreaField;
            if (i == 3) frontArea3Field = frontAreaField;
            if (i == 4) frontArea4Field = frontAreaField;
            // ... 以此类推，为其他前区字段赋值
            if (i == 5) frontArea5Field = frontAreaField;
        }

        for (int i = 1; i <= 2; i++) {
            panel.add(new JLabel("后区" + i + ":"));
            JTextField backAreaField = new JTextField(5);
            panel.add(backAreaField);

            // 将文本字段保存到变量中（如果需要后续访问）
            if (i == 1) backArea1Field = backAreaField;
            if (i == 2) backArea2Field = backAreaField;
        }

        // 创建并添加取消和确认按钮
        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 取消按钮的逻辑（例如关闭窗口）
                dispose();
            }
        });
        panel.add(cancelButton);

        JButton confirmButton = new JButton("确认");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 确认按钮的逻辑（例如收集输入数据并处理）
                // 这里可以添加代码来读取文本字段的值并进行处理
                System.out.println("期号: " + lotteryNumberField.getText());
                ConfigBigLotto configBigLotto=new ConfigBigLotto();
                for(Biglotto biglotto:configBigLotto.getAllbiglotto().getAllData()){
                    if(biglotto.getId()==Integer.parseInt(lotteryNumberField.getText())){
                        JOptionPane.showMessageDialog(null, "期号重复");
                        return;
                    }
                }
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                System.out.println("日期: " + dateField.getText());
                // ... 类似地打印其他字段的值
                int[] dataA=new int[6];
                dataA[5]=0;

                int[] dataB=new int[2];

                dataA[0] = Integer.parseInt(frontArea1Field.getText());
                dataA[1] = Integer.parseInt(frontArea2Field.getText());
                dataA[2] = Integer.parseInt(frontArea3Field.getText());
                dataA[3] = Integer.parseInt(frontArea4Field.getText());
                dataA[4] = Integer.parseInt(frontArea5Field.getText());
                for(int i=0;i<4;++i){
                    for(int j=i+1;j<5;++j){
                        if(dataA[i]>dataA[j]){
                            int v=dataA[i];
                            dataA[i]=dataA[j];
                            dataA[j]=v;
                        }
                    }
                }
                for(int i=0;i<5;++i){
                    if(dataA[i]>35 || dataA[i]<=0){
                        JOptionPane.showMessageDialog(null, "前区号码范围有错误");
                        return;
                    }
                    if(dataA[i]==dataA[i+1]){
                        JOptionPane.showMessageDialog(null, "前区号码存在相同有错误");
                        return;
                    }
                }

                if(Integer.parseInt(backArea1Field.getText())<=0 || Integer.parseInt(backArea2Field.getText())<=0 || Integer.parseInt(backArea1Field.getText())>12 || Integer.parseInt(backArea2Field.getText())>12){
                    JOptionPane.showMessageDialog(null, "后区号码存在范围错误");
                    return;
                }
                if(Integer.parseInt(backArea1Field.getText()) == Integer.parseInt(backArea2Field.getText())){
                    JOptionPane.showMessageDialog(null, "后区号码存在相同有错误");
                    return;
                }
                if(Integer.parseInt(backArea1Field.getText()) < Integer.parseInt(backArea2Field.getText())){
                    dataB[0]=Integer.parseInt(backArea1Field.getText());
                    dataB[1]=Integer.parseInt(backArea2Field.getText());
                }else{
                    dataB[0]=Integer.parseInt(backArea2Field.getText());
                    dataB[1]=Integer.parseInt(backArea1Field.getText());
                }

                Biglotto biglotto=new Biglotto(Integer.parseInt(lotteryNumberField.getText()),dateField.getText(),dataA[0],dataA[1],dataA[2],dataA[3],dataA[4],dataB[0],dataB[1]);
                ConfigBigLotto configbigLotto=new ConfigBigLotto();
                configbigLotto.getAllbiglotto().getAllData().add(biglotto);
                configbigLotto.getAllbiglotto().reorderAscending();
                configbigLotto.SaveBiglottoINFILE();
//                dispose();
                initpanelTable();
            }
        });
        panel.add(confirmButton);

        // 将面板添加到窗口中
        add(panel);

        // 设置窗口可见
        setVisible(true);
    }

    public static void main(String[] args) {
        // 创建并显示窗口
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new addOnelottoframe();
            }
        });
    }
}
